﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Drawing.Printing;
using CrystalDecisions.CrystalReports.Engine;

namespace 生产计划批量打印工具
{
    public partial class frmMain : Form
    {
        string strConnectionString = "Data Source=@ServerAddress;Initial Catalog=@DatabaseName;Persist Security Info=True;User ID=sa;Password=856518armyman";
        string strDefaultSelectStringA = "select jha019 打印标记,jha001 生产计划编号,(select top 1 jhb003 from scjhb_table where jhb001=jha001) 订单编号,(select top 1 jhb024 from scjhb_table where jhb001=jha001) 合同号,jha002 计划日期,(select min(jhb009) from scjhb_table where jhb001=jha001) 最早交货日期,jha014 生产车间,jha016 开票形式,jha017 验收方式,jha020 计划备注,(select top 1 htc002 from TPAMain.dbo.htc_table,scjhb_table where jhb001=jha001 and htc001=jhb003) 订单备注,(select top 1 yh002 from TPAMain.dbo.yh_table ,TPAMain.dbo.hta_table,scjhb_table where yh001=hta023 and hta001=jhb003 and jhb001=jha001) 市场部内勤,PrintTime 打印时间 from scjha_table where jha007='Y'";
        string strDefaultSelectStringB = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jhb001='@JHNo' order by jhb005";
        string strDefaultSelectStringB_1 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020<>'二筛合同' and jhb001='@JHNo' and jhb005 like '%CT41L%' and (jhb005 like 'M-%' OR jhb005 like 'J-%' or jhb005 like 'G-%') order by jhb005";
        string strDefaultSelectStringB_2 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020<>'二筛合同' and jhb001='@JHNo' and jhb005 like '%CC41L%' and (jhb005 like 'M-%' OR jhb005 like 'J-%' or jhb005 like 'G-%') order by jhb005";
        string strDefaultSelectStringB_3 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020<>'二筛合同' and jhb001='@JHNo' and (((jhb005 like '%CT4L%' or jhb005 like '%CC4L%') and (jhb005 like 'M-%' OR jhb005 like 'J-%' or jhb005 like 'G-%')) or jhb005 like 'C-%') order by jhb005";
        string strDefaultSelectStringB_4 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020<>'二筛合同' and jhb001='@JHNo' and jhb005 not like '%LLPF%' and jhb005 not like '%LBPF%' and (jhb005 not like 'C-%' OR (jhb005 like 'C-%' and jhb005 not like '%4L%')) and (jhb005 not like 'M-%' OR (jhb005 like 'M-%' and jhb005 not like '%41L%' and jhb005 not like '%4L%')) and (jhb005 not like 'J-%' OR (jhb005 like 'J-%' and jhb005 not like '%41L%' and jhb005 not like '%4L%')) and (jhb005 not like 'G-%' or (jhb005 like 'G-%' and jhb005 not like '%41L%' and jhb005 not like '%4L%')) order by jhb005";
        string strDefaultSelectStringB_5 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020<>'二筛合同' and jhb001='@JHNo' and (jhb005 like '%LLPF%'or jhb005 like '%LBPF%') order by jhb005";
        string strDefaultSelectStringB_6 = "select jhb002 计划项次,jhb004 订单项次,jhb005 产品编号,jhb008 生产数量,htb008 单价,jhb009 交货日期,htb021+'|'+htb022 执行标准,htb027 用户计划号,htb023 订单备注,htb032 产品备注 from scjhb_table,scjha_table left join TPAMain.dbo.htb_table on jhb003=htb001 and jhb004=htb002 where jha001=jhb001 and jha020='二筛合同' and jhb001='@JHNo' order by jhb005";
        string strUpdatePrintMark = "update scjha_table set jha019='Y',PrintTime=GetDate() where jha001='@JHNo'";
        SqlTools.SqlTools sqt;

        string strFactoryName;

        enum PrinterStatus
        {
            其他状态 = 1,
            未知,
            空闲,
            正在打印,
            预热,
            停止打印,
            打印中,
            离线
        }

        public frmMain(string strTmpFactoryName)
        {
            InitializeComponent();
            strFactoryName = strTmpFactoryName;
            switch (strFactoryName)
            {
                case "601车间":
                case "微波车间":
                    strConnectionString = strConnectionString.Replace("@DatabaseName", "TPAMainTZ");
                    break;
                case "603车间":
                    strConnectionString = strConnectionString.Replace("@DatabaseName", "TPAMain");
                    break;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString().Trim();
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取打印机的当前状态
        /// </summary>
        /// <param name="PrinterDevice">打印机设备名称</param>
        /// <returns>打印机状态</returns>
        private PrinterStatus GetPrinterStat(string PrinterDevice)
        {
            PrinterStatus ret = 0;
            string path = @"win32_printer.DeviceId='" + PrinterDevice + "'";
            ManagementObject printer = new ManagementObject(path);
            printer.Get();
            ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
            return ret;
        }

        /// <summary>
        /// 获取默认打印机
        /// </summary>
        /// <returns>默认打印机名称</returns>
        private string GetDefaultPrinter()
        {
            PrintDocument pd = new PrintDocument();
            return pd.PrinterSettings.PrinterName;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            strConnectionString = strConnectionString.Replace("@ServerAddress", 生产计划批量打印工具.Properties.Settings.Default.ServerAddress);
            sqt = new SqlTools.SqlTools(strConnectionString);
            radioButton4.Checked = true;
            nudPrice_G.Value = 生产计划批量打印工具.Properties.Settings.Default.G_Price_Copies;
            nudNoPrice_G.Value = 生产计划批量打印工具.Properties.Settings.Default.G_NoPrice_Copies;
            nudPrice_J.Value = 生产计划批量打印工具.Properties.Settings.Default.J_Price_Copies;
            nudNoPrice_J.Value = 生产计划批量打印工具.Properties.Settings.Default.J_NoPrice_Copies;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtNoFrom.Enabled = radioButton1.Checked;
            txtNoTo.Enabled = radioButton1.Checked;
            if (radioButton1.Checked)
            {
                radioButton2.Checked = !radioButton1.Checked;
                radioButton3.Checked = !radioButton1.Checked;
                radioButton4.Checked = !radioButton1.Checked;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            dtpDateFrom.Enabled = radioButton2.Checked;
            dtpDateTo.Enabled = radioButton2.Checked;
            if (radioButton2.Checked)
            {
                radioButton1.Checked = !radioButton2.Checked;
                radioButton3.Checked = !radioButton2.Checked;
                radioButton4.Checked = !radioButton2.Checked;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            txtContractNo.Enabled = radioButton3.Checked;
            if (radioButton3.Checked)
            {
                radioButton1.Checked = !radioButton3.Checked;
                radioButton2.Checked = !radioButton3.Checked;
                radioButton4.Checked = !radioButton3.Checked;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                radioButton1.Checked = !radioButton4.Checked;
                radioButton2.Checked = !radioButton4.Checked;
                radioButton3.Checked = !radioButton4.Checked;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int iWorkMode=0;
            string strSelectString = strDefaultSelectStringA;
            if (radioButton1.Checked)
            {
                iWorkMode = 1;
            }
            if (radioButton2.Checked)
            {
                iWorkMode = 2;
            }
            if (radioButton3.Checked)
            {
                iWorkMode = 3;
            }
            if (radioButton4.Checked)
            {
                iWorkMode = 4;
            }
            switch (iWorkMode)
            {
                case 1:
                    if (txtNoFrom.Text.Trim().Length == 0 || txtNoTo.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("起始生产计划号和终止生产计划号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtNoFrom.Focus();
                        return;
                    }
                    strSelectString += " and jha001 between '" + txtNoFrom.Text.Trim() + "' and '" + txtNoTo.Text.Trim() + "'";
                    break;
                case 2:
                    if (dtpDateFrom.Value == dtpDateTo.Value)
                    {
                        strSelectString += " and jha002='" + dtpDateFrom.Value.ToString("yyyyMMdd") + "'";
                    }
                    else
                    {
                        strSelectString += " and jha002 between '" + dtpDateFrom.Value.ToString("yyyyMMdd") + "' and '" + dtpDateTo.Value.ToString("yyyyMMdd") + "'";
                    }
                    break;
                case 3:
                    strSelectString +=" and (select top 1 jhb024 from scjhb_table where jhb001=jha001) like '"+txtContractNo .Text .Trim ()+"'";
                    break ;
            }
            strSelectString += " and (select top 1 jhb024 from scjhb_table where jhb001=jha001) not like '%宏科片%'";
            strSelectString += " and (select top 1 jhb024 from scjhb_table where jhb001=jha001) not like '%送样%'";
            if (chkSearchUnprintRecord .Checked )
            {
                strSelectString +=" and (jha019 is null or jha019='')";
            }
            if (!chkDLScjh.Checked)
            {
                strSelectString += " and (select top 1 jhb003 from scjhb_table where jhb001=jha001)<>''";
            }
            if(strFactoryName =="601车间")
            {
                strSelectString += " and jha014='601车间'";
            }
            else if (strFactoryName == "微波车间")
            {
                strSelectString += " and jha014='微波车间'";
            }
            dgv1.DataSource =sqt.GetDataTable (strSelectString );
            dgv1.Sort(dgv1.Columns["合同号"], ListSortDirection.Ascending);
            dgv1 .AutoResizeColumns ();
            if (dgv1 .Rows.Count >0)
            {
                dgv1 .CurrentCell =dgv1 .Rows [0].Cells [0];
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1 .CurrentRow !=null )
            {
                if (GetString (dgv1 .CurrentRow .Cells ["生产计划编号"].Value ).Length >0)
                {
                    dgv2.DataSource =sqt.GetDataTable (strDefaultSelectStringB .Replace ("@JHNo",GetString (dgv1 .CurrentRow .Cells ["生产计划编号"].Value )));
                    dgv2.AutoResizeColumns ();
                    if (dgv2.Rows.Count > 0)
                    {
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            if (GetString(dgv2.Rows[i].Cells["执行标准"].Value).EndsWith("|"))
                            {
                                dgv2.Rows[i].Cells["执行标准"].Value = GetString(dgv2.Rows[i].Cells["执行标准"].Value).Replace("|", string.Empty);
                            }
                        }
                    }
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)       //多打印任务算法
        {
            if (!chkPrintSelected.Checked)
            {
                btnSelectAll_Click(this, new EventArgs());
            }
            string strDefaultPrinter = GetDefaultPrinter();
            string strPrinter = string.Empty;
            if (dgv1.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    ReportDocument rd = new ReportDocument();
                    if (chkUseType.Checked&&strFactoryName =="603车间")
                    {
                        #region 使用分类打印
                        try
                        {
                            DataRow r1, r2;
                            DataSet ds = new DataSet();
                            #region 单头数据
                            string strMinDeadLine = string.Empty;
                            DataTable dtHeader = new DataTable();
                            dtHeader.TableName = "ReportDataHeader";
                            dtHeader.Columns.Add("标题");
                            dtHeader.Columns.Add("计划单号");
                            dtHeader.Columns.Add("计划日期");
                            dtHeader.Columns.Add("订单编号");
                            dtHeader.Columns.Add("生产车间");
                            dtHeader.Columns.Add("开票形式");
                            dtHeader.Columns.Add("验收方式");
                            dtHeader.Columns.Add("合同号");
                            dtHeader.Columns.Add("交货日期");
                            dtHeader.Columns.Add("打印日期");
                            dtHeader.Columns.Add("打印人");
                            dtHeader.Columns.Add("订单备注");
                            dtHeader.Columns.Add("市场部内勤");
                            //增加订单单头
                            r1 = dtHeader.NewRow();
                            r1["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                            r1["计划日期"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 8);
                            r1["订单编号"] = GetString(dgv1.SelectedRows[i].Cells["订单编号"].Value);
                            r1["生产车间"] = GetString(dgv1.SelectedRows[i].Cells["生产车间"].Value);
                            r1["开票形式"] = GetString(dgv1.SelectedRows[i].Cells["开票形式"].Value);
                            r1["验收方式"] = GetString(dgv1.SelectedRows[i].Cells["验收方式"].Value);
                            r1["合同号"] = GetString(dgv1.SelectedRows[i].Cells["合同号"].Value);
                            if (GetString(r1["合同号"]).Equals(string.Empty))
                            {
                                r1["合同号"] = "独立需求计划";
                            }
                            r1["交货日期"] = GetString(dgv1.SelectedRows[i].Cells["最早交货日期"].Value);
                            strMinDeadLine = GetString(r1["交货日期"]);
                            r1["打印日期"] = DateTime.Now.ToString("yyyy年MM月dd日");
                            r1["打印人"] = 生产计划批量打印工具.Properties.Settings.Default.Operator;
                            r1["订单备注"] = GetString(dgv1.SelectedRows[i].Cells["订单备注"].Value);
                            r1["市场部内勤"] = GetString(dgv1.SelectedRows[i].Cells["市场部内勤"].Value);
                            dtHeader.Rows.Add(r1);
                            ds.Tables.Add(dtHeader);
                            #endregion
                            #region 单身结构
                            DataTable dtBody = new DataTable();
                            dtBody.TableName = "ReportDataBody";
                            dtBody.Columns.Add("计划单号");
                            dtBody.Columns.Add("序号");
                            dtBody.Columns.Add("型号规格");
                            dtBody.Columns.Add("数量");
                            dtBody.Columns.Add("单价");
                            dtBody.Columns.Add("交货日期");
                            dtBody.Columns.Add("执行标准");
                            dtBody.Columns.Add("用户计划号");
                            dtBody.Columns.Add("产品单项备注");
                            dtBody.Columns.Add("订单单项备注");
                            ds.Tables.Add(dtBody);
                            #endregion
                            #region 第一分类：M、G等级CT41L
                            dtHeader .Rows [0] ["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划 - 1";                            
                            DataTable tmpdt = sqt.GetDataTable(strDefaultSelectStringB_1.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    if (chkPrice_G.Checked)
                                    {
                                        //有单价
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //没单价
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                            }
                            #endregion
                            #region 第二分类：M、G等级CC41L
                            dtHeader.Rows[0]["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划 - 2";
                            dtBody.Rows.Clear();
                            tmpdt = sqt.GetDataTable(strDefaultSelectStringB_2.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    if (chkPrice_G.Checked)
                                    {
                                        //有单价
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //没单价
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                            }
                            #endregion
                            #region 第三分类：M、G等级引线产品、按七专工艺生产
                            dtHeader.Rows[0]["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划 - 3";
                            dtBody.Rows.Clear();
                            tmpdt = sqt.GetDataTable(strDefaultSelectStringB_3.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    if (chkPrice_G.Checked)
                                    {
                                        //有单价
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //没单价
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                            }
                            #endregion
                            #region 第四分类：其它
                            dtHeader.Rows[0]["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划 - 4";
                            dtBody.Rows.Clear();
                            tmpdt = sqt.GetDataTable(strDefaultSelectStringB_4.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    if (chkPrice_G.Checked)
                                    {
                                        //有单价
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //没单价
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                            }
                            #endregion
                            #region 第五类：LTCC产品（包含LLPF或包含LBPF）
                            dtHeader.Rows[0]["标题"] = "LTCC" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划";
                            dtBody.Rows.Clear();
                            tmpdt = sqt.GetDataTable(strDefaultSelectStringB_5.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    if (chkPrice_G.Checked)
                                    {
                                        //有单价
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //没单价
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                            }
                            #endregion
                            #region 第六类：二筛合同
                            dtHeader.Rows[0]["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划 - 筛选";
                            dtBody.Rows.Clear();
                            tmpdt = sqt.GetDataTable(strDefaultSelectStringB_6.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                                if (dtHeader.Rows.Count > 0)
                                {
                                    rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                    rd.SetDataSource(ds);
                                }
                                if (rd != null)
                                {
                                    if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                    {
                                        rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                        strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    }
                                    else
                                    {
                                        strPrinter = strDefaultPrinter;
                                    }
                                    while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                    { }
                                    rd.PrintToPrinter(1, true, 0, 0);
                                }
                            }
                            #endregion
                            sqt.DoCmdExecuteNonQuery(strUpdatePrintMark.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                        }
                        catch
                        {
                            continue;
                        }
                        finally
                        {
                            rd.Dispose();
                        }
                        #endregion
                    }
                    else
                    {
                        #region 不使用分类打印
                        try
                        {
                            DataRow r1, r2;
                            DataSet ds = new DataSet();
                            string strMinDeadLine = string.Empty;
                            DataTable dtHeader = new DataTable();
                            dtHeader.TableName = "ReportDataHeader";
                            dtHeader.Columns.Add("标题");
                            dtHeader.Columns.Add("计划单号");
                            dtHeader.Columns.Add("计划日期");
                            dtHeader.Columns.Add("订单编号");
                            dtHeader.Columns.Add("生产车间");
                            dtHeader.Columns.Add("开票形式");
                            dtHeader.Columns.Add("验收方式");
                            dtHeader.Columns.Add("合同号");
                            dtHeader.Columns.Add("交货日期");
                            dtHeader.Columns.Add("打印日期");
                            dtHeader.Columns.Add("打印人");
                            dtHeader.Columns.Add("订单备注");
                            dtHeader.Columns.Add("市场部内勤");
                            DataTable dtBody = new DataTable();
                            dtBody.TableName = "ReportDataBody";
                            dtBody.Columns.Add("计划单号");
                            dtBody.Columns.Add("序号");
                            dtBody.Columns.Add("型号规格");
                            dtBody.Columns.Add("数量");
                            dtBody.Columns.Add("单价");
                            dtBody.Columns.Add("交货日期");
                            dtBody.Columns.Add("执行标准");
                            dtBody.Columns.Add("用户计划号");
                            dtBody.Columns.Add("产品单项备注");
                            dtBody.Columns.Add("订单单项备注");
                            //增加订单单头
                            r1 = dtHeader.NewRow();
                            r1["标题"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划";
                            r1["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                            r1["计划日期"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value).Substring(0, 8);
                            r1["订单编号"] = GetString(dgv1.SelectedRows[i].Cells["订单编号"].Value);
                            r1["生产车间"] = GetString(dgv1.SelectedRows[i].Cells["生产车间"].Value);
                            r1["开票形式"] = GetString(dgv1.SelectedRows[i].Cells["开票形式"].Value);
                            r1["验收方式"] = GetString(dgv1.SelectedRows[i].Cells["验收方式"].Value);
                            r1["合同号"] = GetString(dgv1.SelectedRows[i].Cells["合同号"].Value);
                            if (GetString(r1["合同号"]).Equals(string.Empty))
                            {
                                r1["合同号"] = "独立需求计划";
                            }
                            r1["交货日期"] = GetString(dgv1.SelectedRows[i].Cells["最早交货日期"].Value);
                            strMinDeadLine = GetString(r1["交货日期"]);
                            r1["打印日期"] = DateTime.Now.ToString("yyyy年MM月dd日");
                            r1["打印人"] = 生产计划批量打印工具.Properties.Settings.Default.Operator;
                            r1["订单备注"] = GetString(dgv1.SelectedRows[i].Cells["订单备注"].Value);
                            r1["市场部内勤"] = GetString(dgv1.SelectedRows[i].Cells["市场部内勤"].Value);
                            dtHeader.Rows.Add(r1);
                            //增加单身
                            DataTable tmpdt = sqt.GetDataTable(strDefaultSelectStringB.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                            if (tmpdt.Rows.Count > 0)
                            {
                                for (int k = 0; k < tmpdt.Rows.Count; k++)
                                {
                                    r2 = dtBody.NewRow();
                                    r2["计划单号"] = GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value);
                                    r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                                    r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                                    r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                                    r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                                    if (GetString(tmpdt.Rows[k]["交货日期"]) != strMinDeadLine)
                                    {
                                        r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                                    }
                                    if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                                    }
                                    else
                                    {
                                        r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                                    }
                                    r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                                    r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                                    r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                                    dtBody.Rows.Add(r2);
                                }
                            }
                            else
                            {
                                throw new Exception("单身为空！");
                            }
                            ds.Tables.Add(dtHeader);
                            ds.Tables.Add(dtBody);
                            if (dtHeader.Rows.Count > 0)
                            {
                                rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                                rd.SetDataSource(ds);
                            }
                            if (rd != null)
                            {
                                if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                                {
                                    rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                    strPrinter = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                                }
                                else
                                {
                                    strPrinter = strDefaultPrinter;
                                }
                                while (GetPrinterStat(strPrinter) == PrinterStatus.打印中 || GetPrinterStat(strPrinter) == PrinterStatus.正在打印)
                                { }
                                if (GetString(dgv1.SelectedRows[i].Cells["合同号"].Value).Contains("G"))
                                {
                                    if (chkPrice_G.Checked)
                                    {
                                        //七专有单价打印份数固定1份
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_G.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_G.Checked)
                                    {
                                        //七专没单价打印份数固定3份
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_G.Value), true, 0, 0);
                                    }
                                }
                                else
                                {
                                    if (chkPrice_J.Checked)
                                    {
                                        //普军有单价打印份数固定1份
                                        rd.PrintToPrinter(Convert.ToInt32(nudPrice_J.Value), true, 0, 0);
                                    }
                                    if (chkNoPrice_J.Checked)
                                    {
                                        //普军没单价打印份数固定2份
                                        for (int k = 0; k < ds.Tables["ReportDataBody"].Rows.Count; k++)
                                        {
                                            ds.Tables["ReportDataBody"].Rows[k]["单价"] = string.Empty;
                                        }
                                        rd.SetDataSource(ds);
                                        rd.PrintToPrinter(Convert.ToInt32(nudNoPrice_J.Value), true, 0, 0);
                                    }
                                }
                            }
                            sqt.DoCmdExecuteNonQuery(strUpdatePrintMark.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                        }
                        catch
                        {
                            continue;
                        }
                        finally
                        {
                            rd.Dispose();
                        }
                        #endregion
                    }
                }
                MessageBox.Show("计划打印完毕！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                throw new Exception("未选择任何生产计划！");
            }
        }
        
        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (dgv1.SelectedRows.Count > 1)
                {
                    MessageBox.Show("预览仅显示第一个计划！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                ReportDocument rd = new ReportDocument();
                try
                {
                    DataRow r1, r2;
                    DataSet ds = new DataSet();
                    String strMinDeadLine = string.Empty;
                    DataTable dtHeader = new DataTable();
                    dtHeader.TableName = "ReportDataHeader";
                    dtHeader.Columns.Add("标题");
                    dtHeader.Columns.Add("计划单号");
                    dtHeader.Columns.Add("计划日期");
                    dtHeader.Columns.Add("订单编号");
                    dtHeader.Columns.Add("生产车间");
                    dtHeader.Columns.Add("开票形式");
                    dtHeader.Columns.Add("验收方式");
                    dtHeader.Columns.Add("合同号");
                    dtHeader.Columns.Add("交货日期");
                    dtHeader.Columns.Add("打印日期");
                    dtHeader.Columns.Add("打印人");
                    dtHeader.Columns.Add("订单备注");
                    dtHeader.Columns.Add("市场部内勤");
                    DataTable dtBody = new DataTable();
                    dtBody.TableName = "ReportDataBody";
                    dtBody.Columns.Add("计划单号");
                    dtBody.Columns.Add("序号");
                    dtBody.Columns.Add("型号规格");
                    dtBody.Columns.Add("数量");
                    dtBody.Columns.Add("单价");
                    dtBody.Columns.Add("交货日期");
                    dtBody.Columns.Add("执行标准");
                    dtBody.Columns.Add("用户计划号");
                    dtBody.Columns.Add("产品单项备注");
                    dtBody.Columns.Add("订单单项备注");
                    //增加订单单头
                    r1 = dtHeader.NewRow();
                    r1["标题"] = GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value).Substring(4, 2) + "月生产计划";
                    r1["计划单号"] = GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value);
                    r1["计划日期"] = GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value).Substring (0,8);
                    r1["订单编号"] = GetString(dgv1.SelectedRows[0].Cells["订单编号"].Value);
                    r1["生产车间"] = GetString(dgv1.SelectedRows[0].Cells["生产车间"].Value);
                    r1["开票形式"] = GetString(dgv1.SelectedRows[0].Cells["开票形式"].Value);
                    r1["验收方式"] = GetString(dgv1.SelectedRows[0].Cells["验收方式"].Value);
                    r1["合同号"] = GetString(dgv1.SelectedRows[0].Cells["合同号"].Value);
                    if (GetString(r1["合同号"]).Equals(string.Empty))
                    {
                        r1["合同号"] = "独立需求计划";
                    }
                    r1["交货日期"] = GetString(dgv1.SelectedRows[0].Cells["最早交货日期"].Value);
                    strMinDeadLine = GetString(r1["交货日期"]);
                    r1["打印日期"] = DateTime.Now.ToString("yyyy年MM月dd日");
                    r1["打印人"] = 生产计划批量打印工具.Properties.Settings.Default.Operator;
                    r1["订单备注"] = GetString(dgv1.SelectedRows[0].Cells["订单备注"].Value);
                    r1["市场部内勤"] = GetString(dgv1.SelectedRows[0].Cells["市场部内勤"].Value);
                    dtHeader.Rows.Add(r1);
                    //增加单身
                    DataTable tmpdt = sqt.GetDataTable(strDefaultSelectStringB.Replace("@JHNo", GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value)));
                    if (tmpdt.Rows.Count > 0)
                    {
                        for (int k = 0; k < tmpdt.Rows.Count; k++)
                        {
                            r2 = dtBody.NewRow();
                            r2["计划单号"] = GetString(dgv1.SelectedRows[0].Cells["生产计划编号"].Value);
                            r2["序号"] = GetString(tmpdt.Rows[k]["计划项次"]);
                            r2["型号规格"] = GetString(tmpdt.Rows[k]["产品编号"]);
                            r2["数量"] = GetString(tmpdt.Rows[k]["生产数量"]).Replace(".00", "");
                            r2["单价"] = GetString(tmpdt.Rows[k]["单价"]);
                            if (GetString(tmpdt.Rows [k]["交货日期"]) != strMinDeadLine)
                            {
                                r2["交货日期"] = GetString(tmpdt.Rows[k]["交货日期"]);
                            }
                            if (GetString(tmpdt.Rows[k]["执行标准"]).EndsWith("|"))
                            {
                                r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]).Substring(0, GetString(tmpdt.Rows[k]["执行标准"]).Length - 1);
                            }
                            else
                            {
                                r2["执行标准"] = GetString(tmpdt.Rows[k]["执行标准"]);
                            }
                            r2["用户计划号"] = GetString(tmpdt.Rows[k]["用户计划号"]);
                            r2["产品单项备注"] = GetString(tmpdt.Rows[k]["产品备注"]);
                            r2["订单单项备注"] = GetString(tmpdt.Rows[k]["订单备注"]);
                            dtBody.Rows.Add(r2);
                        }
                    }
                    else
                    {
                        throw new Exception("单身为空！");
                    }
                    ds.Tables.Add(dtHeader);
                    ds.Tables.Add(dtBody);
                    if (dtHeader.Rows.Count > 0)
                    {
                        rd.Load(Application.StartupPath + "\\生产计划单.rpt");
                        rd.SetDataSource(ds);
                    }
                    if (rd != null)
                    {
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.WindowState = FormWindowState.Maximized;
                        newFrmReportPreview.ShowDialog();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                throw new Exception("未选择任何生产计划！");
            }
        }

        private void btnPrintSetting_Click(object sender, EventArgs e)
        {
            frmPrintSettings newFrmPrintSettings=new frmPrintSettings ();
            newFrmPrintSettings.ShowDialog();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Owner.Show();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < dgv1.Rows.Count; i++)
                {
                    dgv1.Rows[i].Selected = true;
                }
            }
        }

        private void 标记为已打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    sqt.DoCmdExecuteNonQuery(strUpdatePrintMark.Replace("@JHNo", GetString(dgv1.SelectedRows[i].Cells["生产计划编号"].Value)));
                }
                btnSearch_Click(this, new EventArgs());
            }
        }

        private void nudPrice_G_ValueChanged(object sender, EventArgs e)
        {
            生产计划批量打印工具.Properties.Settings.Default.G_Price_Copies =(int)nudPrice_G.Value;
            生产计划批量打印工具.Properties.Settings.Default.Save();
        }

        private void nudNoPrice_G_ValueChanged(object sender, EventArgs e)
        {
            生产计划批量打印工具.Properties.Settings.Default.G_NoPrice_Copies = (int)nudNoPrice_G.Value;
            生产计划批量打印工具.Properties.Settings.Default.Save();
        }

        private void nudPrice_J_ValueChanged(object sender, EventArgs e)
        {
            生产计划批量打印工具.Properties.Settings.Default.J_Price_Copies = (int)nudPrice_J.Value;
            生产计划批量打印工具.Properties.Settings.Default.Save();
        }

        private void nudNoPrice_J_ValueChanged(object sender, EventArgs e)
        {
            生产计划批量打印工具.Properties.Settings.Default.J_NoPrice_Copies = (int)nudNoPrice_J.Value;
            生产计划批量打印工具.Properties.Settings.Default.Save();
        }

        private void btnTypeConfig_Click(object sender, EventArgs e)
        {
            frmJhTypeConfig newFrmJhTypeConfig = new frmJhTypeConfig();
            newFrmJhTypeConfig.ShowDialog();
        }

        private void chkUseType_CheckedChanged(object sender, EventArgs e)
        {
            groupBox3.Enabled = !chkUseType.Checked;
            if (chkUseType.Checked)
            {
                groupBox2.Text = "分类打印份数";
            }
            else
            {
                groupBox2.Text = "七专";
            }
        }
    }
}